

import java.io.IOException;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Date;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * Servlet implementation class Overview
 */
@WebServlet("/Overview")
public class Overview extends HttpServlet {
	private static final long serialVersionUID = 1L;
       

    public Overview() {
        super();
    }

	
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		HttpSession ses = request.getSession();
		Database data = null;
		Integer USER_ID = null;
		Integer P_ADDRESS = null;
		Object last = null;
		String datum = null;
		if(ses.getAttribute("USER_ID")==null){
			response.sendRedirect("Login");
			return;
		} else {
			USER_ID = (Integer) ses.getAttribute("USER_ID");
		}
		try {
			data = new Database();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		P_ADDRESS = (Integer) ses.getAttribute("P_ADDRESS");
		if(P_ADDRESS==null && request.getParameter("add")!=null){
			P_ADDRESS = Integer.parseInt(request.getParameter("add"));
		}
		Integer resQuantity = null;
		if(P_ADDRESS!=null){
			try {
				resQuantity = get_current_resources(P_ADDRESS);
			}
			catch (SQLException e) {
				e.printStackTrace();
			}
		} 
		
		try {
			data = new Database();
		} catch (SQLException e1) {
			e1.printStackTrace();
		}
		data.Select("*");
		data.From("PLAYER");
		data.Where("PLAYER.USER_ID="+USER_ID);
		ResultSet result = null;
		try {
			result = data.GetResult();
			if(!result.isBeforeFirst()){
				Database update = new Database();
				update.InsertInto("PLAYER (USER_ID, SCORE)");
				update.Values(USER_ID+",0");
				update.SafeInput().execute();
				update.Close();
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		try {
			result.close();
			data.Close();
			data = new Database();
			ArrayList<Integer> Address = new ArrayList<>();
			ArrayList<Integer> Size = new ArrayList<>();
			ArrayList<Integer> Resources = new ArrayList<>();
			result = data.Select("*").From("POSITION").Where("POSITION.USER_ID ="+USER_ID).GetResult();
			if(!result.isBeforeFirst()){
				Database update = new Database();
				update.Exec("CREATE_NEW_POSITION",Math.round(100+Math.random()*100) + ", " + "500, " + USER_ID).execute();
				//update.InsertInto("POSITION (P_ADDRESS,P_SIZE,USER_ID)").Values("SEQ_ADDRESS.nextval,"+Math.round(100+Math.random()*100)+" ," + USER_ID).SafeInput().execute();
				result.close();
				update.Close();
				data = new Database();
				data.Select("*");
				data.From("POSITION");
				data.Where("POSITION.USER_ID ="+USER_ID);
				result = data.GetResult();
				
			}
			while(result.next()){
				Address.add(result.getInt("P_ADDRESS"));
				Size.add(result.getInt("P_SIZE"));
				
				Resources.add(get_current_resources(result.getInt("P_ADDRESS")));
				
			}
			request.setAttribute("Address", Address);
			request.setAttribute("Size", Size);
			request.setAttribute("Resources", Resources);
			ses.setAttribute("Resources", resQuantity);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		
		
		
		RequestDispatcher View = request.getRequestDispatcher("Overview.jsp");
		View.forward(request, response);
	}
	
	public static int get_current_resources(int address) throws SQLException{
		Database data = new Database();
		ResultSet src = data.Select("*").From("POSITION_INCOME").Where("P_ADDRESS = " + address).GetResult();
		src.next();
		Date d = new Date();
		Timestamp now = new Timestamp(d.getTime());
		Timestamp last_update = src.getTimestamp("LAST_UPDATE");
		int base_res = src.getInt("QUANTITY");
		long interval = (now.getTime() - last_update.getTime())/60000; //60000 ms in minute
		Double income = src.getDouble("Income");
		if(income == null) return base_res;
		
		src.close();
		data.Close();
		int resources = base_res + (int)(income * interval);
		return resources;
	}
	public static int get_resID(int address) throws SQLException{
		Database data = new Database();
		ResultSet src = data.Select("*").From("POSITION_INCOME").Where("P_ADDRESS = " + address).GetResult();
		src.next();
		int result = src.getInt("RES_ID");
		src.close();
		data.Close();
		return result;
	}
	public static void update_resources(int Resources, int resID) throws SQLException{
		Database baza = new Database();
		baza.Update("RESOURCES").Set("QUANTITY = " + Resources + ", LAST_UPDATE = CURRENT_TIMESTAMP(9)").Where("RES_ID = " + resID).execute();
		baza.Close();
		return;
	}
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
	
	}
}
